// *************************************************************************************************
// Include section

// system
#include "project.h"

// driver
#include "display.h"

// logic
#include "banner.h"
#include "user.h"

#include "string.h"

#define BANNER_SIZE 4

// *************************************************************************************************
char * banners[BANNER_SIZE] = {"HAGIHARA", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "0123456789", "ESTE RELOGIO PERTENCE AO HAGI  SE VOCE ENCONTROU ENTAO DEVOLVA"};
struct banner sBanner = {.pos = -1,.string = "BANNER",.index = -1};


void sx_banner(u8 line)
{
	sBanner.index = (sBanner.index + 1) % BANNER_SIZE;
	sBanner.pos = -1;
	sBanner.string = banners[sBanner.index];
}

#pragma optimize=none
void display_banner(u8 line, u8 update)
{
    if (update == DISPLAY_LINE_UPDATE_PARTIAL)
    {
        int len = strlen(sBanner.string);
        int modulo = len + 5;
        sBanner.pos = (sBanner.pos + 1) % modulo;
        unsigned char line[7];
        memset(line, ' ', 6);
        line[6] = '\0';
        int p, start;
        if( sBanner.pos < 5 )
        {
            start = 5 - sBanner.pos;
        }
        else
        {
            start = 1;
        }
        if( sBanner.pos < 5 )
        {
            p = 0;
        }
        else
        {
            p = sBanner.pos - 4;
        }
        for( int i = start; i < 6; i++ )
        {
            if( p < len )
            {
                line[i] = sBanner.string[p];
                p++;
            }
        }
        
        display_chars(LCD_SEG_L2_5_0, line, SEG_ON);
    }
    else if (update == DISPLAY_LINE_CLEAR)
    {
        sBanner.pos = -1;
    }
}